using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;

namespace Quiki.Helpers
{
    [SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
    public static class StringEx
    {
        public static string CropEnd(this string value, int length)
        {
            return value.Substring(0, value.Length - length);
        }

        public static string JoinLines(
            IEnumerable<string> lines,
            int startingLineIndex,
            int howManyLines)
        {
            StringBuilder s = new StringBuilder();

            int i = 0;
            foreach (string line in lines)
            {
                if (i >= startingLineIndex && i < startingLineIndex + howManyLines)
                {
                    if (i < startingLineIndex + howManyLines - 1)
                        s.AppendLine(line);
                    else
                        s.Append(line);
                }

                i++;
            }

            return s.ToString();
        }

        public static IList<string> SplitIntoLines(this string value)
        {
            List<string> lines = new List<string>();
            using (StringReader reader = new StringReader(value))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                    lines.Add(line);
            }

            return lines;
        }

        public static IList<string> SplitIntoWords(this string value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            List<string> words = new List<string>();

            int i = 0;
            int? wordStart = null;
            bool isInQuotes = false;
            while (i <= value.Length)
            {
                if (i == value.Length)
                {
                    if (isInQuotes)
                        throw new ArgumentException("Missing end quotes");

                    if (wordStart != null)
                    {
                        string word = value.Substring(wordStart.Value, i - wordStart.Value);
                        words.Add(word);
                    }

                    break;
                }

                if (value[i] == ' ' && false == isInQuotes)
                {
                    if (wordStart != null)
                    {
                        string word = value.Substring(wordStart.Value, i - wordStart.Value);
                        words.Add(word);
                        wordStart = null;
                    }
                }
                else if (value[i] == '\"')
                {
                    if (false == isInQuotes)
                    {
                        if (wordStart != null)
                        {
                            string word = value.Substring(wordStart.Value, i - wordStart.Value);
                            words.Add(word);
                            wordStart = null;
                        }

                        isInQuotes = true;
                        wordStart = i;
                    }
                    else
                    {
                        string word = value.Substring(wordStart.Value + 1, i - (wordStart.Value + 1));
                        if (word.Length > 0)
                            words.Add(word);
                        isInQuotes = false;
                        wordStart = null;                        
                    }
                }
                else
                {
                    if (wordStart == null)
                        wordStart = i;
                }

                i++;
            }

            return words;
        }
    }
}